import { ENUM_CLIP, ENUM_UI } from "../GbzEnum";
import GbzInstance from "../GbzInstance";
import GbzAudioManager from "../manager/GbzAudioManager";
import GbzDataManager from "../manager/GbzDataManager";
import GbzSdkManager from "../manager/GbzSdkManager";
import BaseLayer from "./GbzBaselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzSettingLayer extends BaseLayer {

    panel: cc.Node = null
    btn_music: cc.Node = null
    btn_sound: cc.Node = null
    btn_close: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btn_music = cc.find('buttons/btn_music', this.panel)
        this.btn_music.on('click', this.onMusic, this)
        this.btn_sound = cc.find('buttons/btn_sound', this.panel)
        this.btn_sound.on('click', this.onSound, this)
        this.btn_close = cc.find('btn_close', this.panel)
        this.btn_close.on('click', this.onClose, this)
    }

    onDestroy() {
        this.btn_music.off('click', this.onMusic, this)
        this.btn_sound.off('click', this.onSound, this)
        this.btn_close.off('click', this.onClose, this)
    }

    onEnable() {
        this.zoom(this.panel)
        this.rendor()
        GbzSdkManager.ins.showInter()
        GbzSdkManager.ins.showCustomRow(true)
    }

    onDisable() {
        GbzSdkManager.ins.showCustomRow(false)
    }

    rendor() {
        this.btn_music.getChildByName('on').active = GbzDataManager.ins.game_music
        this.btn_music.getChildByName('off').active = !GbzDataManager.ins.game_music
        this.btn_sound.getChildByName('on').active = GbzDataManager.ins.game_sound
        this.btn_sound.getChildByName('off').active = !GbzDataManager.ins.game_sound
    }

    onClose() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_SETTING, false)
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_MENU)
    }

    onSound() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzDataManager.ins.game_sound = !GbzDataManager.ins.game_sound
        GbzDataManager.ins.store()
        this.rendor()
    }

    onMusic() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzDataManager.ins.game_music = !GbzDataManager.ins.game_music
        GbzDataManager.ins.store()
        if (GbzDataManager.ins.game_music) {
            GbzAudioManager.ins.play()
        } else {
            GbzAudioManager.ins.stop()
        }
        this.rendor()
    }
}
